<?php

namespace app\admin\controller;

use app\admin\model\Admin as AdminModel;
use think\facade\Request;
use app\api\model\StoreModel;
use think\Validate;
use think\exception\Exception;
class Admin extends Common
{
    public function index(Request $request, AdminModel $model)
    {
        $data = $request::param();
        $where = [];
        if (!empty($data['name'])) {
            $where[] = ['username|name', 'like', '%' . $data['name'] . '%'];
        }
        if (!empty($data['status'])) {
            $where[] = ['status', '=', $data['status']];
        }
        if (!empty($data['store'])) {
            $where[] = ['store', '=', $data['store']];
        }
        $list = $model->where($where)->page($data['page'], $data['size'])->order('id desc')->paginate($data['size'])
            ->each(function($v){
                $v['create_time'] = date('Y-m-d',$v['create_time']);
                $v['store'] = StoreModel::where('id',$v['store'])->value('name');
                $v['end_time'] = isset($v['end_time']) ? date('Y-m-d',$v['end_time']) : '';
                $v['entry_time'] = isset($v['entry_time']) ? date('Y-m-d',$v['entry_time']) : '';
                $v['status'] = $v['status'] == 1 ? '启用' : '禁用';
                return $v;
            });
        return $this->returnJson(200, 'ok', $list);
    }

    public function add(Request $request, AdminModel $model)
    {
        $data = $request::param();
        $validate = Validate::make([
            'username'  => 'require|mobile',
            'password' => 'require',
            'name' => 'require'
        ]);
        if (!$validate->check($data)) {
            return $this->returnJson(500, $validate->getError());
        }
        $user = $model->where('username',$data['username'])->find();
        if(!empty($user)){
            return $this->returnJson(500, '账号已存在');
        }
        $data['password'] = MD5($data['password']);
        $data['entry_time'] = strtotime($data['entry_time']);
        $data['create_time'] = time();
        $data['level'] = 3;
        $data['status'] = 1;
        $res = $model->Add($data);
        if (!empty($res)) {
            return $this->returnJson(200, 'success');
        } else {
            return $this->returnJson(500, 'fail');
        }
    }

    public function info($id, Request $request, AdminModel $model)
    {
        $res = $model->getAdminInfo($id);
        if (!empty($res)) {
            return $this->returnJson(200, 'success',$res);
        } else {
            return $this->returnJson(500, 'fail');
        }
    }

    public function edit($id, Request $request, AdminModel $model)
    {
        $data = $request::param();
        $validate = Validate::make([
            'username'  => 'require|mobile',
            // 'password' => 'require',
            'name' => 'require'
        ]);
        if (!$validate->check($data)) {
            return $this->returnJson(500, $validate->getError());
        }
        $user = $model->where('username',$data['username'])->whereNotIN('id', [$id])->find();
        if(!empty($user)){
            return $this->returnJson(500, '账号已存在');
        }
        //$data['password'] = MD5($data['password']);
        $data['entry_time'] = strtotime($data['entry_time']);
        $res = $model->Edit($id, $data);
        if (!empty($res)) {
            return $this->returnJson(200, 'ok');
        } else {
            return $this->returnJson(500, 'fail');
        }
    }

    public function del($id, Request $request, AdminModel $model)
    {
        try {
            $res = $model->del($id);
            if ($res) {
                return $this->returnJson(200, 'ok');
            } else {
                return $this->returnJson(500, 'fail');
            }
        } catch (\Exception $e) {
            return $this->returnJson(500, $e->getMessage());
        }
    }
}